﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace 第三次作业.utils
{
    internal class FileUtil
    {
        private string filePath { get; set; }

        private string fileName { get; }

        public FileUtil(string filePath)
        {
            this.filePath = filePath;
            this.fileName = filePath;
        }

        public FileUtil()
        {
            this.filePath = "";
            this.fileName = "";
        }

        public string[] readFile()
        {
           return File.ReadAllLines(filePath);
        }

        public static string[] readFile(string filePath)
        {
            return File.ReadAllLines(filePath);
        }

        // 格式化文件
        public string[] formatFile()
        {
            string[] lines = File.ReadAllLines(this.filePath);

            List<string> cleanedLines = new List<string>();
            foreach (string line in lines)
            {
                string cleanedLine = line.Trim();

                if (string.IsNullOrWhiteSpace(cleanedLine) || cleanedLine.StartsWith("//"))
                {
                    continue;
                }
                cleanedLine = Regex.Replace(cleanedLine, @"\/\/.*", "");
                cleanedLines.Add(cleanedLine);
            }

            return cleanedLines.ToArray();

        }

        // 计数行数
        public int lineNum()
        {
            string[] lines = File.ReadAllLines(this.filePath);

            return lines.Length;
        }

        public static int lineNum(string[] lines)
        {
            return lines.Length;
        }

        // 计数词数
        public int countWords()
        {
            string content = File.ReadAllText(this.filePath);
            MatchCollection matches = Regex.Matches(content, @"\S+");
            return matches.Count;
        }

        public static int countWords(string content)
        {
            MatchCollection matches = Regex.Matches(content, @"\S+");
            return matches.Count;
        }

        // 统计词频
        public Dictionary<string, int> countWordOccurrences()
        {
            string[] lines = File.ReadAllLines(this.filePath);

            Dictionary<string, int> wordOccurrences = new Dictionary<string, int>();

            foreach (string line in lines)
            {
                string[] wordsInLine = line.Split(' ', '\t');
                foreach (string word in wordsInLine)
                {
                    string cleanedWord = Regex.Replace(word, "[^a-zA-Z0-9]", "");
                    if (!string.IsNullOrWhiteSpace(cleanedWord))
                    {
                        if (wordOccurrences.ContainsKey(cleanedWord))
                        {
                            wordOccurrences[cleanedWord]++;
                        }
                        else
                        {
                            wordOccurrences.Add(cleanedWord, 1);
                        }
                    }
                }
            }

            return wordOccurrences;
        }

        public static Dictionary<string, int> countWordOccurrences(string[] lines)
        {
            Dictionary<string, int> wordOccurrences = new Dictionary<string, int>();

            foreach (string line in lines)
            {
                string[] wordsInLine = line.Split(' ', '\t');
                foreach (string word in wordsInLine)
                {
                    string cleanedWord = Regex.Replace(word, "[^a-zA-Z0-9]", "");
                    if (!string.IsNullOrWhiteSpace(cleanedWord))
                    {
                        if (wordOccurrences.ContainsKey(cleanedWord))
                        {
                            wordOccurrences[cleanedWord]++;
                        }
                        else
                        {
                            wordOccurrences.Add(cleanedWord, 1);
                        }
                    }
                }
            }

            return wordOccurrences;
        }

    }
}
